package com.tjmntv.tj.myapplication.demo9

import android.media.PlaybackParams
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import com.tjmntv.tj.myapplication.R
import kotlinx.android.synthetic.main.activity_kotlin_demo9.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class KotlinDemo9 : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_demo9)

        val videoPath = "android.resource://$packageName/${R.raw.video1}"
        videoView.setVideoPath(videoPath)
//        videoView.setMediaController(MediaController(this))
        videoView.setOnPreparedListener{
            progressBar2.visibility = View.INVISIBLE
            progressBar.max = it.duration
            it.isLooping = true // 循环播放
            it.playbackParams = PlaybackParams().apply {
                // speed = 2f  // 2倍速度播放
            }
            it.start()
        }
        lifecycleScope.launch {
            while (true){
                if (videoView.isPlaying){
                    progressBar.progress = videoView.currentPosition
                }
                delay(500)
            }
        }
//        videoView.start()
    }
}